package com.frkj.yuan.activity;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.frkj.yuan.R;
import com.frkj.yuan.adapter.BaseRecyclerHolder;
import com.frkj.yuan.adapter.BaseRecyclerRefreshAdapter;
import com.frkj.yuan.base.BaseActivity;
import com.frkj.yuan.data.YuyueKeshiDataUtils;
import com.frkj.yuan.dialog.SelectYouHuiQuanDialog;
import com.frkj.yuan.utils.FullScreenUtils;
import com.frkj.yuan.view.RecyclerViewLayoutManager;

import java.io.IOException;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.OnClick;
import cn.ycbjie.ycstatusbarlib.bar.YCAppBar;

/**
 * 作者：菲菲 on 2022/2/23 17:21
 * company：丰蓉信息科技有限公司
 */
public class SubmitPayOrderActivity extends BaseActivity {
    @BindView(R.id.base_title)
    TextView mTitleMsg;
    @BindView(R.id.down_time)
    TextView mDownTime;
    private RecyclerView mUserRecyclerView;
    private RecyclerView mNoUserRecyclerView;
    private int minute = 30;//这是分钟
    private int second = 0;//这是分钟后面的秒数。这里是以30分钟为例的，所以，minute是30，second是0
    private Timer timer;
    private TimerTask timerTask;
    //这是接收回来处理的消息
    private Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            if (minute == 0) {
                if (second == 0) {
                    mDownTime.setText("Time out !");
                    if (timer != null) {
                        timer.cancel();
                        timer = null;
                    }
                    if (timerTask != null) {
                        timerTask = null;
                    }
                } else {
                    second--;
                    if (second >= 10) {
                        mDownTime.setText("0" + minute + ":" + second);
                    } else {
                        mDownTime.setText("0" + minute + ":0" + second);
                    }
                }
            } else {
                if (second == 0) {
                    second = 59;
                    minute--;
                    if (minute >= 10) {
                        mDownTime.setText(minute + ":" + second);
                    } else {
                        mDownTime.setText("0" + minute + ":" + second);
                    }
                } else {
                    second--;
                    if (second >= 10) {
                        if (minute >= 10) {
                            mDownTime.setText(minute + ":" + second);
                        } else {
                            mDownTime.setText("0" + minute + ":" + second);
                        }
                    } else {
                        if (minute >= 10) {
                            mDownTime.setText(minute + ":0" + second);
                        } else {
                            mDownTime.setText("0" + minute + ":0" + second);
                        }
                    }
                }
            }
        }

    };

    @Override
    public int getLayoutId() {
        return R.layout.activity_submit_payorder;
    }

    @Override
    public void initLayoutView() {
        mTitleMsg.setText("支付订单");
        initDownTime();
        FullScreenUtils.getInstance().fullScreen(SubmitPayOrderActivity.this,true);

    }
    private void initDownTime() {
        mDownTime.setText(minute + ":" + second);

        timerTask = new TimerTask() {

            @Override
            public void run() {
                Message msg = new Message();
                msg.what = 0;
                handler.sendMessage(msg);
            }
        };

        timer = new Timer();
        timer.schedule(timerTask, 0, 1000);
    }

    @OnClick({R.id.base_back, R.id.submit_pay, R.id.linar_youhui})
    public void OnClick(View view) {
        switch (view.getId()) {
            case R.id.base_back:
                finishWitchAnimation();
                break;
            case R.id.submit_pay:
                openActivityWitchAnimation(PayOrderSuucessActivity.class);
                break;
            case R.id.linar_youhui:
                SelectYouhuiQuan();
                break;

        }
    }

    private void SelectYouhuiQuan() {
        SelectYouHuiQuanDialog dialog = new SelectYouHuiQuanDialog(SubmitPayOrderActivity.this);
        dialog.show();
        mUserRecyclerView = (RecyclerView) dialog.findViewById(R.id.use_recyclerVeiw);
        mNoUserRecyclerView = (RecyclerView) dialog.findViewById(R.id.no_recycler);
        RecyclerViewLayoutManager.setLinearVerticalManager(SubmitPayOrderActivity.this, mUserRecyclerView);
        RecyclerViewLayoutManager.setLinearVerticalManager(SubmitPayOrderActivity.this, mNoUserRecyclerView);
        List<String> mLeftList = YuyueKeshiDataUtils.mUserYou();
        List<String> mRightList = YuyueKeshiDataUtils.mNoser();
        BaseRecyclerRefreshAdapter mUserAdapter = new BaseRecyclerRefreshAdapter<String>(SubmitPayOrderActivity.this, mLeftList, R.layout.user_youhui_item) {
            @Override
            public void convert(BaseRecyclerHolder holder, String item, int position, boolean isScrolling) throws IOException {

            }
        };
        mUserRecyclerView.setAdapter(mUserAdapter);
        mUserAdapter.setOnItemClickListener(new BaseRecyclerRefreshAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(RecyclerView parent, View view, int position) {
                dialog.dismiss();
            }
        });
        BaseRecyclerRefreshAdapter mNoAdapter = new BaseRecyclerRefreshAdapter<String>(SubmitPayOrderActivity.this, mRightList, R.layout.user_youhui_item) {
            @Override
            public void convert(BaseRecyclerHolder holder, String item, int position, boolean isScrolling) throws IOException {
                holder.getView(R.id.linar_user).setBackgroundResource(R.drawable.no_user_bg);
            }
        };
        mNoUserRecyclerView.setAdapter(mNoAdapter);
        mNoAdapter.setOnItemClickListener(new BaseRecyclerRefreshAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(RecyclerView parent, View view, int position) {
                dialog.dismiss();
            }
        });


    }
    @Override
    protected void onDestroy() {
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
        if (timerTask != null) {
            timerTask = null;
        }
        minute = -1;
        second = -1;
        super.onDestroy();
    }
}
